package com.yang.servlet;

import com.yang.service.CategoryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/CategoryStatusChangeServlet")
public class CategoryStatusChangeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CategoryService categoryService = new CategoryService();
        int categoryId = Integer.parseInt(request.getParameter("categoryId"));
        String oper = request.getParameter("oper");
        boolean b = false;
        if("stop".equals(oper)){
            //调用停用方法
            try {
                b = categoryService.disableCategoryStatus(categoryId);

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }else if("start".equals(oper)){
            try {
                //调用启用方法
                b = categoryService.enableCategoryStatus(categoryId);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        String jsonStr = b ? "{\"code\":1000}":"{\"code\":1001}";
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.println(jsonStr);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
